package com.zm.demo.core.leetcode;

import java.util.Arrays;
import java.util.Stack;

public class MinStack1 {

    int []ary = new int[8];
    int []minAry = new int[8];
    int count = -1;
    int min = Integer.MAX_VALUE;
    /** initialize your data structure here. */
    public MinStack1() {

    }

    public void push(int x) {
        if (count++ >= ary.length){
            int [] tmp = Arrays.copyOf(ary, 2*ary.length);
            ary = tmp;
            int [] minTmp = Arrays.copyOf(minAry, 2*ary.length);
            minAry = minTmp;
        }
        min = Math.min(min,x);
        minAry[count] = min;
        ary[count] = x;
    }

    public void pop() {
        count--;
        if (minAry[count] > min){
            min = minAry[count];
        }
        if (count<0){
            min = Integer.MAX_VALUE;
        }
    }

    public int top() {
        return ary[count];
    }

    public int getMin() {
        return minAry[count];
    }

    /**
     * ["MinStack","push","push","getMin","getMin","push","getMin","getMin","top","getMin","pop","push","push","getMin","push","pop","top","getMin","pop"]
     * [[],[-10],[14],[],[],[-20],[],[],[],[],[],[10],[-7],[],[-7],[],[],[],[]]
     * @param args
     */
    public static void main(String[] args){
        MinStack1 minStack = new MinStack1();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        minStack.getMin();
        minStack.pop();
        minStack.top();
        minStack.getMin();

    }
}